package com.team24.hotel.controller;

import com.team24.hotel.domain.Role;
import com.team24.hotel.domain.User;
import com.team24.hotel.service.RoleService;
import com.team24.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/getAll")
    public String getAllUserAndRoleName(Model model){
        List<User> userList=userService.findAllUserAndRoleName();
        model.addAttribute("userList",userList);
        return "userList";
    }

    @RequestMapping("/add")
    public String addUser(Model model){
        List<Role> roleList=roleService.findAll();
        model.addAttribute("user",new User());
        model.addAttribute("roleList",roleList);
        return "userAdd";
    }

    @RequestMapping("/edit")
    public String editInfoPrint(String id,Model model){
        User user=userService.findByPrimaryKey(id);
        List<Role> roleList=roleService.findAll();
        model.addAttribute("user",user);
        model.addAttribute("roleList",roleList);
        return "userAdd";
    }

    @RequestMapping("/save")
    public String saveUser(User user,String stringUserBirthday){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=simpleDateFormat.parse(stringUserBirthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setUserBirthday(date);
        userService.saveOrUpdate(user);
        return "redirect:/user/getAll";
    }

    @RequestMapping("/delete")
    public String deleteUser(String id){
        int del=userService.deleteUser(id);
        if(del==1) return "redirect:/user/getAll";
        else return "404";
    }

    @RequestMapping(value = "/checkRegister",method = RequestMethod.POST)
    @ResponseBody
    public boolean checkRegister(HttpServletRequest request){
        String tel = request.getParameter("tel");
        User t=userService.findUserByTel(tel);
        if(t==null){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/checkRegister1",method = RequestMethod.POST)
    @ResponseBody
    public boolean checkRegister1(HttpServletRequest request) {
        String email = request.getParameter("email");
        User e = userService.findUserByEmail(email);
        if (e == null) {
            return true;
        } else {
            return false;
        }
    }

}
